package ar.com.android.alco.providers;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ar.com.android.alco.dbHelpers.SQLALCOHelper;
import ar.com.android.alco.entities.Version;

public class VersionProvider {
	
	private static final String DB_COLUMN_0_NAME = "idVersion";
	private static final String DB_COLUMN_1_NAME = "version";

	private SQLiteDatabase database;
	private SQLALCOHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME};
	
	public VersionProvider(Context context) {
		dbHelper = SQLALCOHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
		
	public long insertVersion(Version version)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_0_NAME, version.getIdVersion());
		contentValues.put(DB_COLUMN_1_NAME, version.getVersion());
		
		long insertId = database.insert(SQLALCOHelper.DB_VERSION_TABLE_NAME, null, contentValues);
		return insertId;
	}
	
	public boolean removeVersion(Integer idVersion)
	{
		int result = database.delete(SQLALCOHelper.DB_VERSION_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idVersion, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeVersion()
	{
		int result = database.delete(SQLALCOHelper.DB_VERSION_TABLE_NAME, null, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
		
	public long updateVersion(Version version)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, version.getVersion());
		
		return database.update(SQLALCOHelper.DB_VERSION_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + version.getIdVersion(), null);
	}
	
	public Version getVersion(Integer idVersion)
	{
		try
		{
			Cursor cursor = database.query(SQLALCOHelper.DB_VERSION_TABLE_NAME, allColumns, DB_COLUMN_0_NAME + " = " + idVersion, null, null, null, null);
			cursor.moveToFirst();
			
			Version version = new Version();
			version.setIdVersion(cursor.getInt(0));
			version.setVersion(cursor.getInt(1));

			cursor.close();
			return version;
	
		}
		catch (Exception e) {
			return null;
		}		
	}
	
	public List<Version> getVersiones() {
		List<Version> lstVersion = new ArrayList<Version>();
		
		Cursor cursor = database.query(SQLALCOHelper.DB_VERSION_TABLE_NAME, allColumns, null, null, null, null, null);
		cursor.moveToFirst();
		
		while (!cursor.isAfterLast()) {
			Version version = new Version();
			version.setIdVersion(cursor.getInt(0));
			version.setVersion(cursor.getInt(1));
			
			lstVersion.add(version);
			cursor.moveToNext();
		}
		cursor.close();
		return lstVersion;
	}
	
	public Boolean getTableEmpty() {
		Cursor cur = database.rawQuery("SELECT COUNT(*) FROM " + SQLALCOHelper.DB_VERSION_TABLE_NAME, null);
		if (cur != null) {
		    cur.moveToFirst();
		    if (cur.getInt (0) == 0)
		        return Boolean.TRUE;
		    else
		    	return Boolean.FALSE;
		}
		else
			return Boolean.TRUE;
			
	}
	
	public Version getLastVersion()
	{
		try
		{
			Cursor cursor = database.query(SQLALCOHelper.DB_VERSION_TABLE_NAME, allColumns, null, null, null, null, DB_COLUMN_1_NAME + " DESC");
			cursor.moveToFirst();
			
			Version version = new Version();
			version.setIdVersion(cursor.getInt(0));
			version.setVersion(cursor.getInt(1));

			cursor.close();
			return version;
	
		}
		catch (Exception e) {
			return null;
		}		
	}
}
